<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/8/14
 * Time: 17:10
 * 商品列表
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\BuyRecord;
use app\common\model\GoodsRecord;
use think\Db;

class Goods extends Api {
	protected $noNeedLogin = ['*'];
	protected $noNeedRight = ['*'];
	
	
	/**
	 * 基数列表
	 * @param page 页数
	 * @param sort 排序
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index(){
		//页数
		$page = input('post.page', 1, 'int');
		//排序
		$sort = input('post.sort');
		
		//默认排序为最热
		$order = ['r.buy_number' => 'desc'];
		switch ($sort) {
			case 1:
				//最新
				$order = ['r.create_time' => 'desc'];
				break;
			case 2:
				//最快
				$order = ['r.buy_number' => 'desc'];
				break;
			case 3:
				//最贵
				$order = ['r.total_number' => 'desc'];
				break;
			case 4:
				//最易中
				$order = ['r.total_number' => 'asc'];
				break;
			default:;
		}
		
		
		$model = new GoodsRecord();
		$rows = $model->getRows($page, $order);
		$this->success(__('查询成功'), $rows);
		
	}
	
	/**
	 * 商品详情
	 * @param id 期数ID
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function detail() {
		$id = input('post.id', '', 'int');
		
		if(!$id || $id < 0){
			$this->error(__('参数错误'));
		}
		
		$model = new GoodsRecord();
		$row = Db::name('goodsRecord')->alias('r')
			->join('goods g', 'r.goods_id = g.id', 'left')
			->where(['r.id' => $id])
			->field('r.id, r.total_number, r.goods_id, r.buy_number, g.goods_name, g.banner_images, r.status, r.winning_id, r.open_time, g.content')
			->find();
		if(!$row) {
			$this->error(__('数据不存在'));
		}
		
		//轮播图切割成数组
		$row['banner_images'] = explode(',', $row['banner_images']);
		
		//开奖详情
		$openDetail = [];
		if($row['status'] == $model::STATUS_F) {
			//查找中奖的用户信息及购买信息
			$openDetail = Db::name('BuyRecord')->alias('b')
				->join('user u', 'b.user_id = u.id', 'left')
				->where('goods_record_id', '=', $row['id'])
				->where('FIND_IN_SET(\''.$row['winning_id'].'\', no_list)')
				->field('u.avatar, u.username, b.ip, b.user_id')
				->find();
			$openDetail['record_no'] = $row['id'];
			$openDetail['winning_no'] = $row['winning_id'];
			$openDetail['open_time'] = date('Y-m-d H:i:s', $row['open_time']);
			
			//购买数量
			$openDetail['buy_number'] = Db::name('BuyRecord')
				->where(['goods_recoed_id' => $row['id'], 'user_id' => $openDetail['user_id']])->sum('number');
		}
		
		$returnData = [
			'goods' => $row,
			'openDetail' => $openDetail
		];
		
		$this->success(__('查询成功'), $returnData);
	}
	
	
	/**
	 * 期数参与记录
	 * @param id 期数ID
	 * @param page 页数
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function buyDetail(){
		$id = input('post.id', '', 'int');
		$page = input('post.page', 1, 'int');
		if(!$id || $id < 0) {
			$this->error(__('参数错误'));
		}
		
		$rows = Db::name('BuyRecord')->alias('b')
			->join('user u', 'b.user_id = u.id', 'left')
			->where(['b.goods_record_id' => $id])
			->page($page)
			->field('u.avatar, u.username, b.id, b.number, from_unixtime(create_time) as time, b.ip')
			->order(['b.create_time' => 'DESC'])
			->select();
		
		$this->success(__('查询成功'), $rows);
	}
	
	/**
	 * 商品中奖记录
	 * @param goods_id 商品ID
	 * @param page 页数
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function historyOpen() {
		$goodsId = input('post.goods_id', '', 'int');
		if(!$goodsId || $goodsId < 0) {
			$this->error(__('参数错误'));
		}
		
		$page = input('post.page', 1, 'int');
		
		$buyModel = new BuyRecord();
		
		$rows = Db::name('GoodsRecord')->alias('r')
			->join('buyRecord b', 'r.id = b.goods_record_id', 'left')
			->join('user u', 'b.user_id = u.id', 'left')
			->where(['r.goods_id' => $goodsId, 'b.status' => $buyModel::STATUS_Y])
			->field('r.id, from_unixtime(r.open_time) as time, u.id as uid, u.avatar, u.username, b.ip, r.winning_id, b.number')
			->page($page)
			->select();
		
		$this->success(__('查询成功'), $rows);
	}
	
	/**
	 * 商品评价记录
	 * @param goods_id 商品ID
	 * @param page 页数
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function comments(){
		$goodsId = input('post.goods_id', '', 'int');
		if(!$goodsId || $goodsId < 0) {
			$this->error(__('参数错误'));
		}
		
		$page = input('post.page', 1, 'int');
		
		$rows = Db::name('Comments')->alias('c')
			->join('goodsRecord r', 'c.goods_record_id = r.id', 'left')
			->join('user u', 'c.user_id = u.id', 'left')
			->join('goods g', 'c.goods_id = g.id', 'left')
			->where(['c.goods_id' => $goodsId])
			->field('c.content, c.images, from_unixtime(c.create_time) as time, u.avatar, u.username, g.goods_name')
			->page($page)
			->select();
		
		if($rows) {
			foreach($rows as $k => $v) {
				$rows[$k]['images'] = explode(',', $v['images']);
			}
		}
		
		$this->success(__('查询成功'), $rows);
	}
	
	
	/**
	 * 开奖记录
	 * @param page 页数
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function openList(){
		$page = input('post.page', 1, 'int');
		$buyModel = new BuyRecord();
		
		$rows = Db::name('GoodsRecord')->alias('r')
			->join('buyRecord b', 'r.id = b.goods_record_id', 'left')
			->join('user u', 'b.user_id = u.id', 'left')
			->join('goods g', 'r.goods_id = g.id', 'left')
			->where(['b.status' => $buyModel::STATUS_Y])
			->field('r.id, from_unixtime(r.open_time) as time, u.avatar, u.username, b.ip, u.id, r.winning_id, b.number, r.winning_id, g.goods_name, g.image')
			->order(['r.open_time' => 'DESC'])
			->page($page)
			->select();
		
		$this->success(__('查询成功'), $rows);
	}
}
